<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Eloquent\CompanyRepository;
use \Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;


class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function Login(Request $request){
        $user_name = strip_tags(trim($request->get('user_name')));
        $password  = strip_tags(trim($request->get('pwd')));
        $resource  = CompanyRepository::first(['account'=>$user_name]);
        if($resource == null){
            return response(['status'=>false,'msg'=>'账号不存在']);
        }
        if(Hash::check($password,$resource->password)){
            $request->session()->put('user',$resource->toArray());
            return response(['status'=>true,'msg'=>'登陆成功','url'=>'/home']);
        }
        else
            return response(['status'=>false,'msg'=>'密码错误']);
    }

    public function SignOut(Request $request){
        $request->session()->flush();
        return redirect('/index');
    }
}
